package cn.beatuty.makeup.module.system.controller.admin.hotproducts;

import cn.beatuty.makeup.framework.common.pojo.CommonResult;
import cn.beatuty.makeup.framework.common.pojo.PageParam;
import cn.beatuty.makeup.framework.common.pojo.PageResult;
import cn.beatuty.makeup.framework.common.util.object.BeanUtils;
import cn.beatuty.makeup.framework.excel.core.util.ExcelUtils;
import cn.beatuty.makeup.framework.operatelog.core.annotations.OperateLog;
import cn.beatuty.makeup.module.system.controller.admin.hotproducts.vo.HotProductsPageReqVO;
import cn.beatuty.makeup.module.system.controller.admin.hotproducts.vo.HotProductsRespVO;
import cn.beatuty.makeup.module.system.controller.admin.hotproducts.vo.HotProductsSaveReqVO;
import cn.beatuty.makeup.module.system.dal.dataobject.hotproducts.HotProductsDO;
import cn.beatuty.makeup.module.system.service.hotproducts.HotProductsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.beatuty.makeup.framework.common.pojo.CommonResult.success;
import static cn.beatuty.makeup.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 商品")
@RestController
@RequestMapping("/system/hot-products")
@Validated
public class HotProductsController {

    @Resource
    private HotProductsService hotProductsService;

    @PostMapping("/create")
    @Operation(summary = "创建热门商品")
    public CommonResult<Long> createHotProducts(@Valid @RequestBody HotProductsSaveReqVO createReqVO) {
        return success(hotProductsService.createHotProducts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新热门商品")
    public CommonResult<Boolean> updateHotProducts(@Valid @RequestBody HotProductsSaveReqVO updateReqVO) {
        hotProductsService.updateHotProducts(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除热门商品")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHotProducts(@RequestParam("id") Long id) {
        hotProductsService.deleteHotProducts(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得热门商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HotProductsRespVO> getHotProducts(@RequestParam("id") Long id) {
        HotProductsDO hotProducts = hotProductsService.getHotProducts(id);
        return success(BeanUtils.toBean(hotProducts, HotProductsRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得热门商品分页")
    public CommonResult<PageResult<HotProductsRespVO>> getHotProductsPage(@Valid @RequestBody HotProductsPageReqVO pageReqVO) {
        if (pageReqVO.getIsAllData() && pageReqVO.getIsHot()){
            pageReqVO.setPageSize(6);
            pageReqVO.setProductsStatus("0");
            pageReqVO.setSort("1");
        }
        if (pageReqVO.getIsAllData() && !pageReqVO.getIsHot()){
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
            pageReqVO.setProductsStatus("0");
            pageReqVO.setSort("0");
        }
        PageResult<HotProductsDO> pageResult = hotProductsService.getHotProductsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HotProductsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出热门商品 Excel")
    @OperateLog(type = EXPORT)
    public void exportHotProductsExcel(@Valid HotProductsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HotProductsDO> list = hotProductsService.getHotProductsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "热门商品.xls", "数据", HotProductsRespVO.class,
                        BeanUtils.toBean(list, HotProductsRespVO.class));
    }

    @GetMapping("/trend")
    @Operation(summary = "统计趋势")
    public CommonResult<List<Map<String, Object>>> getArticlePublishTrend() {
        return success(hotProductsService.countTotalPriceByDayInLast7Days());
    }

}